<template>
  <div>
    <navtop title="新增地址" path="/address"></navtop>
    <div>
      <van-address-edit
        :area-list="areaList"
        :address-info="list"
        show-delete
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
      />
    </div>
  </div>
</template>

<script>
import area from "../../../static/data/area";
import navtop from "./navtop";
import { Toast } from "vant";
export default {
  components: {
    navtop,
  },
  created() {
    this.areaList = area.AddressInfo;
  },
  data() {
    return {
      areaList: {},
      searchResult: [],
      addressdata: [],
      list:
        {
          name:'李佳',
          tel:'15874901564',
          address:'湖南岳阳评奖'

        }
      
    };
  },
  methods: {
    //保存信息
    onSave(content) {
      Toast.success("保存成功");
      let oldaddressdata = JSON.parse(localStorage.getItem("addressdata"));
      
      let obj = {
        
        isDefault: content.isDefault,
        name: content.name,
        tel: content.tel,
        address: content.city+" " + content.county+" " + content.addressDetail,
      };
      if(oldaddressdata!=null){
        oldaddressdata.forEach(element => {
          this.addressdata.push(element)
        });
      }
      // let chooseid;
      if(content.isDefault){
        this.addressdata.forEach(item => {
            item.isDefault='';
            // chooseid=item.id
        });
        
        // localStorage.setItem('chosenAddressId',chooseid)
        // console.log(this.addressdata);
      }

      this.addressdata.unshift(obj); 
      

      console.log(this.addressdata);
      console.log(oldaddressdata);
      localStorage.setItem("addressdata", JSON.stringify(this.addressdata));
       
      this.$router.push('/address');
    },

    //删除信息
    onDelete() {
      Toast("delete");
    },
    onChangeDetail(val) {
      if (val) {
        console.log(val);
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
};
</script>

<style lang="less" scoped>
.van-address-item__name {
  font-size: 24px;
}
</style>